﻿using AxMSTSCLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace RemoteManger.Remote
{
    public partial class Remote : Form
    {
        AxMsRdpClient8NotSafeForScripting rdpClient = null;
        private string ip { get; set; }
        private string userName { get; set; }
        private int port { get; set; }
        private string password { get; set; }
        public Remote(string _ip,string _userName, int _port, string _password)
        {
            InitializeComponent();
            Load += Form1_Load;
            //this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            ip= _ip;
            userName = _userName;
            port = _port;
            password = _password;


        }
        private void Form1_Load(object sender, EventArgs e)
        {
            StartRemot(ip, userName, port, password);

        }

        public void  StartRemot(string ip, string userName, int port, string password)
        {
            rdpClient = new AxMsRdpClient8NotSafeForScripting();
            this.Text = "服务器：" + ip+"    状态：正在连接";
            this.Controls.Add(rdpClient);
            rdpClient.Dock = DockStyle.Fill;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.DesktopWidth = Screen.PrimaryScreen.Bounds.Width;
            rdpClient.DesktopHeight = Screen.PrimaryScreen.Bounds.Height- 30;

            // 设置远程桌面连接参数
            rdpClient.Server = ip;  // 替换为实际远程服务器IP
            rdpClient.UserName = userName;
            rdpClient.AdvancedSettings9.ClearTextPassword = password;
            rdpClient.AdvancedSettings9.RDPPort = port;
            rdpClient.OnConnected += RdpClient_OnConnected;
            rdpClient.OnDisconnected += RdpClient_OnDisconnected;
            rdpClient.Connect();


        }

        private void RdpClient_OnConnected(object sender, EventArgs e)
        {
            this.Text = "服务器：" + ip + "    状态：已连接";
            MessageBox.Show("远程桌面已成功连接！");
        }

        private void RdpClient_OnDisconnected(object sender, IMsTscAxEvents_OnDisconnectedEvent e)
        {
          //  MessageBox.Show("远程桌面已断开连接！");
            this.Close();
        }
    }
    
}
